import "package:flutter/material.dart";
import "package:flutter/rendering.dart";
import "../widget/keepAliveWrapper.dart"; //缓存组件

class PageViewPage extends StatelessWidget {
  final int _count = 6; //页面数量
  const PageViewPage({super.key});
  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];
    for (int index = 0; index < _count; index++) {
      children.add(KeepAliveWrapper(child: Page(index: index)));
    }
    return Scaffold(
      appBar: AppBar(
        title: const Text('PageViewPage'),
      ),
      body: PageView(
        allowImplicitScrolling: true, //允许前后缓存一页
        children: children,
      ),
    );
  }
}

class Page extends StatefulWidget {
  final int index;
  const Page({super.key, required this.index});
  @override
  State<StatefulWidget> createState() => PageState();
}

// // 只有State 可以 混入 AutomaticKeepAliveClientMixin
// class PageState extends State<Page> with AutomaticKeepAliveClientMixin {
//   @override
//   Widget build(BuildContext context) {
//     print(widget.index);
//     return Center(
//       child: Text('${widget.index}'),
//     );
//   }

//   @override
//   bool get wantKeepAlive => true; //表示开启keepAlive缓存
// }

// 只有State 可以 混入 AutomaticKeepAliveClientMixin
class PageState extends State<Page> {
  @override
  Widget build(BuildContext context) {
    print(widget.index);
    return Center(
      child: Text('${widget.index}'),
    );
  }
}
